<template>
    <div class="app-container">
        <!-- 搜索区 -->
        <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
            <el-form-item label="产品名称" prop="product">
                <el-input v-model="queryParams.product" placeholder="请输入产品名称" clearable size="small"
                    style="width: 200px">
                </el-input>
            </el-form-item>
            <el-form-item label="反馈人" prop="feedbackPerson">
                <el-input v-model="queryParams.feedbackPerson" placeholder="请输入反馈人" clearable size="small"
                    style="width: 200px">
                </el-input>
            </el-form-item>
            <el-form-item label="接收人" prop="recipient">
                <el-input v-model="queryParams.recipient" placeholder="请输入接收人" clearable size="small"
                    style="width: 200px">
                </el-input>
            </el-form-item>
            <el-form-item label="反馈日期" prop="feedbackDate">
                <el-date-picker v-model="queryParams.feedbackDate" type="date" placeholder="选择日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="处理日期" prop="disposeDate">
                <el-date-picker v-model="queryParams.disposeDate" type="date" placeholder="选择日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['set:model:add']">新增</el-button>
            </el-col>

            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="zlwtList">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="成品质检单号" prop="processNumber" width="120" align="center" />
            <el-table-column label="问题分类" prop="questionClassification" :show-overflow-tooltip="true" width="150"
                align="center" />
            <el-table-column label="产品名称" prop="product" :show-overflow-tooltip="true" width="150" align="center" />
            <el-table-column label="问题原因" prop="problem" :show-overflow-tooltip="true" width="150" align="center" />
            <el-table-column label="反馈人" prop="feedbackPerson" :show-overflow-tooltip="true" width="150"
                align="center" />
            <el-table-column label="反馈日期" prop="feedbackDate" :show-overflow-tooltip="true" width="100"
                align="center" />
            <el-table-column label="解决方案" prop="solution" :show-overflow-tooltip="true" width="100" align="center" />
            <el-table-column label="处理结果" prop="publicSub" :show-overflow-tooltip="true" width="100" align="center" />
            <el-table-column label="接收人" prop="recipient" :show-overflow-tooltip="true" width="100" align="center" />
            <el-table-column label="处理日期" prop="disposeDate" :show-overflow-tooltip="true" width="100" align="center" />
            <el-table-column label="操作" align="center" width="280" fixed="right">
                <template slot-scope="scope">
                    <el-button size="mini" v-hasPermi="['set:model:edit']" type="primary" icon="el-icon-edit" title="编辑"
                        @click="handleUpdate(scope.row)">编辑</el-button>
                    <el-button size="mini" v-hasPermi="['set:model:remove']" type="danger" icon="el-icon-delete"
                        title="删除" @click="handleDelete(scope.row)">删除</el-button>
                    <el-link type="primary" style="margin: 0 0 0 10px;" @click="detail(scope.row)">详情</el-link>


                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="110px">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="成品质检单号" prop="processNumber">
                            <el-input v-model="form.processNumber" placeholder="请输入成品质检单号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="问题分类" prop="questionClassification">
                            <el-input v-model="form.questionClassification" placeholder="请输入问题分类" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="产品名称" prop="product">
                            <el-input v-model="form.product" placeholder="请输入产品名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="问题原因" prop="problem">
                            <el-input v-model="form.problem" placeholder="请输入问题原因" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="反馈人" prop="feedbackPerson">
                            <el-input v-model="form.feedbackPerson" placeholder="请输入反馈人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="解决方案" prop="solution">
                            <el-input v-model="form.solution" placeholder="请输入解决方案" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="处理结果" prop="publicSub">
                            <el-input v-model="form.publicSub" placeholder="请输入处理结果" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="接收人" prop="recipient">
                            <el-input v-model="form.recipient" placeholder="请输入接收人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="创建时间" prop="createTime">
                            <el-date-picker v-model="form.createTime" type="date" placeholder="选择创建时间"
                                value-format="yyyy-MM-dd">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 车型导入对话框 -->
        <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
            <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
                :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">
                    将文件拖到此处，或
                    <em>点击上传</em>
                </div>
                <!-- <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的车型数据
            <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
          </div> -->
                <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入.png,.jpg,.xls,.xlsx,.doc格式文件！</div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button @click="upload.open = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>

    import { getToken } from "@/utils/auth";
    import { getZlwtList, addZlwt, uploadZlwt, delZlwt, updateZlwt } from "@/api/mass/zlwt";

    export default {
        name: "set_model",
        data() {
            return {
                //检验日期
                valueDate: '',
                // 状态字典
                statusOptions: [],
                //检验记录
                inspectionRecordOptions: [],
                // 车型导入参数
                upload: {

                    // 是否显示弹出层（用户导入）
                    open: false,
                    // 弹出层标题（用户导入）
                    title: "",
                    // 是否禁用上传
                    isUploading: false,
                    // 是否更新已经存在的用户数据
                    updateSupport: 0,
                    // 设置上传的请求头部
                    headers: { Authorization: "Bearer " + getToken() },
                    // 上传的地址
                    url: process.env.VUE_APP_BASE_API + "/set/model/importData",
                },
                // 遮罩层
                loading: true,
                // 选中数组
                modelCodes: [],
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 表格数据
                zlwtList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 是否显示弹出层（数据权限）
                openDataScope: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    modelDesLocal: undefined,
                    modelCode: undefined,
                },
                // 表单参数
                form: {},
                defaultProps: {
                    children: "children",
                    label: "label",
                },
                // 表单校验
                rules: {
                    modelCode: [
                        { required: true, message: "库位不能为空", trigger: "blur" },
                    ],
                    modelDes: [
                        { required: true, message: "车型描述不能为空", trigger: "blur" },
                    ],
                    modelType: [
                        { required: true, message: "车型大类不能为空", trigger: "blur" },
                    ],
                },
            };
        },
        created() {
            this.getList();
            // this.getDicts("sys_normal_disable").then((response) => {
            //   this.statusOptions = response.data;
            // });
            this.getDicts("cp_jyd_status").then((response) => {
                this.statusOptions = response.data;
            });
        },
        methods: {

            /** 导入按钮操作 */
            handleImport() {
                this.upload.title = "上传成品检验单";
                this.upload.open = true;
            },
            /** 下载模板操作 */
            importTemplate() {
                importTemplate().then((response) => {
                    this.download(response.msg);
                });
            },
            // 文件上传中处理
            handleFileUploadProgress(event, file, fileList) {
                this.upload.isUploading = true;
            },
            // 文件上传成功处理
            handleFileSuccess(response, file, fileList) {
                this.upload.open = false;
                this.upload.isUploading = false;
                this.$refs.upload.clearFiles();
                this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
                // this.getList();
            },
            // 提交上传文件
            submitFileForm() {
                this.$refs.upload.submit();
            },
            /** 查询列表 */
            getList() {
                this.loading = true;
                getZlwtList(this.addDateRange(this.queryParams, this.dateRange)).then(
                    (response) => {
                        this.zlwtList = response.rows;
                        this.total = response.total;
                        this.loading = false;
                    }
                );
            },

            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
                this.getList();
            },

            // 表单重置
            reset() {
                this.form = {
                    processNumber: undefined, processNumber: undefined, product: undefined,
                    problem: undefined,
                    feedbackPerson: undefined,
                    solution: undefined,
                    publicSub: undefined,
                    recipient: undefined,
                    createTime: undefined,
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.dateRange = [];
                this.resetForm("queryForm");
                this.handleQuery();
            },

            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加质量问题单";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                this.open = true;
                this.title = "修改质量问题单";
                this.form = row
            },

            /** 提交按钮 */
            submitForm: function () {
                this.$refs["form"].validate((valid) => {
                    if (valid) {
                        if (this.title === "修改质量问题单") {
                            updateZlwt(this.form).then((response) => {
                                if (response.code === 200) {
                                    this.msgSuccess("修改成功");
                                    this.open = false;
                                    this.getList();
                                }
                            });
                        } else {
                            addZlwt(this.form).then((response) => {
                                if (response.code === 200) {
                                    this.msgSuccess("新增成功");
                                    this.open = false;
                                    this.getList();
                                }
                            });
                        }
                    }
                });
            },
            /** 提交按钮（数据权限） */
            submitDataScope: function () {
                if (this.form.id != undefined) {
                    dataScope(this.form).then((response) => {
                        if (response.code === 200) {
                            this.msgSuccess("修改成功");
                            this.openDataScope = false;
                            this.getList();
                        }
                    });
                }
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                this.$confirm(
                    '是否确认删除产品名称为"' + row.processNumber + '"的数据项?',
                    "警告",
                    {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }
                )
                    .then(() => {
                        delZlwt(row.id);
                    })
                    .then(() => {
                        this.getList();
                        this.msgSuccess("删除成功");
                    })
                    .catch(function () { });
            },
            /** 导出按钮操作 */
            handleExport() {
                const queryParams = this.queryParams;
                this.$confirm("是否确认下载?", "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(function () {
                        return exportZlwt(queryParams);
                    })
                    .then((response) => {
                        this.download(response.msg);
                    })
                    .catch(function () { });
            },
            /* 详情 */
            detail() {
                this.$router.push('/cangku/rukuGen/ruku')
            }
        },
    };
</script>